package com.lin.community_thumb_sys.controller;

import cn.hutool.http.server.HttpServerRequest;
import com.lin.community_thumb_sys.common.*;
import com.lin.community_thumb_sys.model.domain.User;
import com.lin.community_thumb_sys.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @version 1.0
 * @Author linzixiong
 * @Data 2025/8/15  14:00
 * @description：用户相关接口
 */
@RestController
@Slf4j
@RequestMapping("user")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/login")
    public BaseResponse<User> userLogin(Long userId , HttpServletRequest request){
        //使用throwUtils校验用户ID
        ThrowUtils.throwIf(userId == null, ErrorCode.PARAMS_ERROR);
        User user = userService.getById(userId);
        ThrowUtils.throwIf(user == null, ErrorCode.OPERATION_ERROR);
        //将用户对象保存session
        request.getSession().setAttribute(UserConstant.LOGIN_USER_SESSION_KEY,user);
        return ResultUtils.success(user);
    }

    @GetMapping("/get/login")
    public BaseResponse<User> getLoginUser(HttpServletRequest request) {
        User loginUser = (User) request.getSession().getAttribute(UserConstant.LOGIN_USER_SESSION_KEY);
        return ResultUtils.success(loginUser);
    }

}
